Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
An Ethical Hacker has been tasked to find vulnerabilities in Transaction Operations of an
Organization XYZ. He has been able to find a relation between the Transaction ID and the Key generated
by the System. Write a program that generates Key from the Transaction ID.
Transaction ID S is a String consisting of only lower case alphabetic characters, i.e. S[i] ∈ [a, z]. K
is said to be a possible key of S, if there exist at least one-character C ∈ [a, z] in each and every substring
of S of length K. Key of ID is the minimum of such possible Ks.
Input
First line contains Transaction ID S (1≤ length(S)≤ 100000)
Output
Key of Transaction ID i.e. Minimum of possible keys Ks
Like if inputs are:-
zbcde   /*here c is common between substrings 
        Having 3 max length i.e zbc cde
bbbbb    /* in this only b is there so length 
         Becomes 1 i.e b
zbzczbz  /*here z is common in maximum length 
         2 of given string i.e zb bz zc cz zb 
          bz 
Then output will be like:-
3
1
2


What I have tried:

<pre>import java.util.Scanner;

public class lab3hw {
	public static boolean aresame(int[] array)
	{
	    boolean fe = array[0] == 0;
	    for(int i = 1; i < array.length; i++)
	    {
	        if(fe)
	            if(array[i] != 0) return false;
	        else 
	            if(!(array[0]==array[i])) return false;
	    }

	    return true;
	}
	public static boolean samechar(String s)
	{
	    int n = s.length();
	    boolean boo=false;
	    for (int i = 1; i < n; i++)
	        if (s.substring(i,i+1).equals(s.substring(0,1))) {
	            boo=false;
	            
	        }else {
	        	boo=true;
	        }
	    return boo;
	}
	public static int conditions(String str) {  
	 int ci, i, j, k, l=0;
	 int result=0;
     char c, ch;
     i=str.length();
     if(samechar(str)==false) {
    	 
    	 result=1;
     }else {
	     for(c='A'; c<='z'; c++)
	     {
	         k=0;
	         for(j=0; j<i; j++)
	         {
	             ch = str.charAt(j);
	             if(ch == c)
	             {
	                 k++;
	             }
	         }
	         if(k>0)
	         {
	             int[]n=new int[50];
	             for(int m=0;m<i;m++){
	             n[m]=k;
	             }
	         if(aresame(n)==true) { 
	        	 if((str.length())%2==0) {
	        		 result= str.length()/2;}
	        	 else {
	        		 result=(str.length()+1)/2;
	        	 }
	         }else if(aresame(n)==false) {
	        	 
	         }
	         }
	     }
	     
     }
     return result;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner a=new Scanner(System.in);
		String b=a.next();
		System.out.println(conditions(b));
		
	}

}

Please tell me how can i do
Posted
Updated 23-Jan-18 16:46pm
v3
Comments
OriginalGriff 23-Jan-18 10:35am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And that makes no sense whatsoever!
Please, look at your homework question again, and try to work out exactly what it is asking: we get no context beyond exactly what you type.

Use the "Improve question" widget to edit your question and provide better information.
Gamomusic 23-Jan-18 10:40am    
now i have provided the better information

1 solution

This is your homework, so you'll get no code!
But ... this isn't complicated, particularly.
Start by finding out the frequency of each possible value in the input: there are a number of ways to do that, but for such a simple input, I'd probably use an array to count them: first element for 'a', second for 'b', and so forth. Parse your string, and count each character. When you know that, you can find out the highest frequency: all '1's means it's the middle character, only one value is obvious, others means you need to process the line. You can use CharAt to access individual characters.

Start with that, and see how far you get.
 
Share this answer
 
Comments
Gamomusic 23-Jan-18 22:48pm    
now my code is working for 2 cases but i'm still confused how to do the last test case that is zbzczbz for this output will be 2.
OriginalGriff 24-Jan-18 5:52am    
Yes - because the highest frequency will be 3, which is for 'z'. So each string starts or ends with a 'z'.
string one: start to z, (0 chars, ignore)
string two: z to just-before-z, zb (2 chars)
string three: after z to z, bz (2 chars)
And so on.
Longest string: 2 chars.
Gamomusic 24-Jan-18 1:37am    
please tell me what should i do for last case
Gamomusic 24-Jan-18 1:38am    
now i have change my code what i have tried

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900